package com.wswy.wzcx.ui.main.community.detail

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.KeyboardUtils
import com.che.libcommon.api.page.PageAdapterWrapper
import com.che.libcommon.api.page.Pagination
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.Func1
import com.wswy.wzcx.module.base.loadLayout
import com.wswy.wzcx.ui.main.community.CommentVO
import com.wswy.wzcx.ui.main.community.CommunityViewHolders
import com.wswy.wzcx.ui.main.community.TopicVO
import kotlinx.android.synthetic.main.topic_comment_left_label_item.view.*

class CommentAdapter (private val holderLoginBlock : ((Func1)->Unit?)?,
                      private val replyClickListener : View.OnClickListener?=null,
                      pagination: Pagination<Any?>?): PageAdapterWrapper<Any, RecyclerView.ViewHolder>(pagination) {

    companion object {

        private const val TYPE_LABEL = 10021

    }


    override fun getItemViewType(position: Int): Int {

        val itemData = getItemData(position)
        return when(itemData){

            is TopicVO -> itemData.type

            is CommentVO -> CommunityViewHolders.TYPE_COMMENT_1

            is String -> TYPE_LABEL

            else -> CommunityViewHolders.TYPE_COMMENT_1
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return when(viewType){
            CommunityViewHolders.TYPE_TOPIC_1 -> CommunityViewHolders.SimpleTopicViewHolder(loadLayout(parent,R.layout.topic_item_simple),holderLoginBlock,replyClickListener)
            in CommunityViewHolders.TYPE_TOPIC_IMG_1..CommunityViewHolders.TYPE_TOPIC_IMG_9 -> CommunityViewHolders.SimpleImgViewHolder(viewType-CommunityViewHolders.BASE_IMG_TYPE,
                    loadLayout(parent,R.layout.topic_item_simple),holderLoginBlock,replyClickListener)

            CommunityViewHolders.TYPE_COMMENT_1 -> CommunityViewHolders.CommentViewHolder(loadLayout(parent,R.layout.topic_comment_item))

            TYPE_LABEL -> LabelViewHolder(loadLayout(parent,R.layout.topic_comment_left_label_item))

            else -> CommunityViewHolders.CommentViewHolder(loadLayout(parent,R.layout.topic_comment_item))

        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, data: Any?) {
         when(holder){
             is CommunityViewHolders.SimpleTopicViewHolder-> holder.bind(data as TopicVO)

             is CommunityViewHolders.CommentViewHolder-> holder.bind(data as CommentVO)

             is LabelViewHolder -> {

                 val count = data?.toString()?.toIntOrNull()?:0
                 if (count > 0){
                     holder.itemView.ll_empty.visibility = View.GONE
                     holder.itemView.tv_comment_count.text = holder.itemView.context.getString(R.string.comment_count,data?.toString())
                 }else{
                     holder.itemView.ll_empty.visibility = View.VISIBLE
                     holder.itemView.tv_comment_count.text = holder.itemView.context.getString(R.string.comment_count_empty)
                 }


             }
         }
    }


    private class LabelViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView)

}